package com.controller;

import com.config.ExceptionConfig;
import com.entity.Users;
import com.service.CartService;
import com.service.GoodService;
import com.service.OrderService;
import com.service.UserService;
import com.util.SafeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Objects;


/**
 * 前台用户控制器
 */
@Controller
@RequestMapping("/index")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private CartService cartService;

    @Resource
    private GoodService goodService;

    @Resource
    private OrderService orderService;
    /**
     * 前台用户注册
     * @return
     */
    @GetMapping("/register")
    public String reg(){
        return "/index/register.jsp";
    }

/**
 * 用户注册
 */
    @PostMapping("/register")
    public String register(Users users, HttpServletRequest request){
        if (users.getUsername().isEmpty()){
            request.setAttribute("msg","用户名不能为空");
        }else if (Objects.nonNull(userService.getByUsername(users.getUsername()))){
            request.setAttribute("msg","用户已存在");
        }else {
            userService.add(users);
            request.setAttribute("msg","注册成功 可以登录了");
            return "/index/login.jsp";
        }
        return "/index/register.jsp";
    }

    /**
     * 用户登录
     * @return
     */
    @GetMapping("/login")
    public String log() {return "/index/login.jsp";}

    @PostMapping("/login")
    public String login(Users user, HttpServletRequest request, HttpSession session){
        Users loginUser = userService.getByUsernameAndPassword(user.getUsername(),user.getPassword());
        if (Objects.isNull(loginUser)){
            request.setAttribute("msg","用户名或密码错误");
            return "/index/login.jsp";
        }
        session.setAttribute("user",loginUser);
        session.setAttribute("cartCount",cartService.getCount(loginUser.getId()));
        String referer=request.getHeader("referer");
        System.out.println(referer);
        return "redirect:index";
    }

    @GetMapping("/address")
    private String address(){
        return "/index/address.jsp";
    }

    @PostMapping("/addressUpdate")
    private String addressUpdate(String name,String phone,String address,HttpServletRequest request,HttpSession session){
        Users user= (Users) session.getAttribute("user");
        userService.update(user.getId(),name,phone,address);
        session.setAttribute("user",userService.get(user.getId()));
        request.setAttribute("msg","信息修改成功！");
        return "/index/address.jsp";
    }

    @GetMapping("/password")
    private String password(){
        return "/index/password.jsp";
    }

    @PostMapping("/passwordUpdate")
    private String passwordUpdate(String password,String passwordNew,HttpServletRequest request,HttpSession session){
        Users user= (Users) session.getAttribute("user");
        user=userService.get(user.getId());
        if (!user.getPassword().equals(SafeUtil.encode(password))){
            request.setAttribute("msg","原密码错误！");
        }else{
            userService.updatePassword(user.getId(),passwordNew);
            request.setAttribute("msg","密码修改成功！");
        }
        return "/index/password.jsp";
    }

    /**
     * 查看购物车
     * @param request
     * @param session
     * @return
     */
    @GetMapping("/cart")
    public String cart(HttpServletRequest request,HttpSession session){
        Users user= (Users) session.getAttribute("user");
        request.setAttribute("cartList",cartService.getList(user.getId()));
        request.setAttribute("cartCount",cartService.getCount(user.getId()));
        request.setAttribute("cartTotal",cartService.getTotal(user.getId()));
        return "/index/cart.jsp";
    }

    /**
     * 购物车总金额
     */
    @GetMapping("/cartTotal")
    public @ResponseBody int cartTotal(HttpSession session){
        Users user= (Users) session.getAttribute("user");
        return cartService.getTotal(user.getId());
    }

    /**
     * 添加数量
     * @param id
     * @return
     */
    @PostMapping("cartAdd")
    public @ResponseBody boolean cartAdd(int id){ return cartService.add(id);}

    /**
     * 减少数量
     * @param id
     * @return
     */
    @PostMapping("cartLess")
    public @ResponseBody boolean cartLess(int id){return cartService.less(id);}

    @PostMapping("cartDelete")
    public @ResponseBody boolean cartDelete(int id){
        return cartService.delete(id);
    }

    @PostMapping("/cartBuy")
    public @ResponseBody boolean cartBuy(int goodId,HttpSession session){
        Users user= (Users) session.getAttribute("user");
        return cartService.save(goodId,user.getId());
    }

    

    @GetMapping("/orderSave")
    public String orderSave(ServletRequest request, HttpSession session) throws ExceptionConfig.MyException {
        Users user= (Users) session.getAttribute("user");
        int orderId=orderService.save(user.getId());
        session.removeAttribute("cartCount");
        return "redirect:orderPay?id="+orderId;
    }

    /**
     * 支付页面
     * @param request
     * @param id
     * @return
     */
    @GetMapping("/orderPay")
    public String orderSave(int id,ServletRequest request){
        request.setAttribute("order",orderService.get(id));
        return "/index/pay.jsp";
    }

}
